package com.andy.qhb.controller;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.andy.qhb.base.PageUtil;
import com.andy.qhb.base.PageVO;
import com.andy.qhb.base.RespVO;
import com.andy.qhb.base.table.PageStructure;
import com.andy.qhb.base.table.Search;
import com.andy.qhb.base.table.TableHeader;
import com.andy.qhb.base.table.Tool;
import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.dao.vo.Where;
import com.andy.qhb.entity.UsrDayRept;
import com.andy.qhb.entity.UsrTixianLog;
import com.andy.qhb.entity.UsrUser;
import com.andy.qhb.utils.DateUtil;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("agent")
public class AgentController extends BaseController{

	@RequestMapping("")  
    public ModelAndView index() {  
		UsrUser user = (UsrUser) getUser();
		
		Map<String,Object> map = Maps.newHashMap();
		
		map.put("id", user.getId());
		map.put("puid", user.getPuid());
		
		map.put("gnum", DaoUtils.getChildCountByPr("usr_user", user.getId()));
		
		map.put("xiaji", DaoUtils.count("select count(1) from usr_user where puid=?", user.getId()));
		
		Long start = DateUtil.getCurrentDateLong();
		Long end = start + 86400;
		map.put("jrxz", DaoUtils.count("select count(1) from usr_user where puid=? and (create_time>? and create_time<?)", 
				user.getId(),start,end));
		map.put("jrhy", DaoUtils.count("select count(1) from usr_user where puid=? and (login_time>? and login_time<?)", 
				user.getId(),start,end));
		
		
        return createMV("agent/dashboard","我的推广", Collections.singletonMap("formData", map));
    }  
	
	/**
	 * 用户列表：结构
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="user/list")
	public ModelAndView user() throws Exception {
		TableHeader tableHeader = new TableHeader();
		tableHeader.setNames(new String[]{"id", "nick_name",  "gnum","xiaji","role"});
		tableHeader.setTexts(new String[]{"ID", "玩家昵称","团队人数","直属玩家","类型"});
		
		Tool tool = new Tool();
		tool.setUrls("agent/user/wbdfy");
		tool.setTexts("设置为代理");
		tool.setTypes("del");
		
		Search search = new Search();
		search.setNames(new String[]{"uid"});
		search.setTexts(new String[]{"玩家ID"});
		search.setTypes(new String[]{"text"});
		
		PageStructure data = PageUtil.createTablePageStructure("agent/user/listData", "id", tableHeader,tool,search);
		return createMV("tableList","我的玩家", Collections.singletonMap("formStruct", data));
	}
	
	/**
	 * 用户列表：数据
	 * @param page
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="user/listData")
	public RespVO userData(Integer page,Integer limit, String uid) throws Exception {
		PageVO users = null;
		if(StringUtils.isNotBlank(uid)){
			users = DaoUtils.getPageForMap(UsrUser.table(), null,page,limit,Where.eq("id", uid),Where.eq("puid", getUserId()));
		}else{
			users = DaoUtils.getPageForMap(UsrUser.table(), null,page,limit,Where.eq("puid", getUserId()));
		}
		
		int week = DateUtil.dayOfWeek(new Date());
		Date dayw = DateUtil.addDay(new Date(), 7-week);
		String dayws = DateUtil.getDateStr(dayw);
		users.getItems().forEach((it)->{
//			it.put("tdgx", DaoUtils.count("select chou_coin from usr_day_rept where day>? and type=1 and uid "
//					+ "in (select u.id from usr_user u where u.puid=?)",dayws, it.get("id")));
//			it.put("wjgx", DaoUtils.count("select my_coin from usr_day_rept where day=? and type=1 and uid "
//					+ "in (select u.id from usr_user u where u.puid=?)",dayws, it.get("id")));
			
			it.put("gnum", DaoUtils.getChildCountByPr("usr_user", it.get("id")));
			it.put("xiaji", DaoUtils.count("select count(1) from usr_user where puid=?", it.get("id")));
			it.put("role", MapUtils.getInteger(it, "agent",0) > 0?"代理":"玩家");
		});
		
//		idToName(users.getItems(), UsrGameInfo.table(), "id#uid:tdgx,wjgx");
		
        return RespVO.createSuccessJsonResonse(users);
	}
	
	/**
	 * 我的业绩：结构
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="yeji/list")
	public ModelAndView yeji() throws Exception {
		TableHeader tableHeader = new TableHeader();
		tableHeader.setNames(new String[]{"create_time:date", "all_coin:yuan", "chou_coin:yuan","my_coin:yuan"});
		tableHeader.setTexts(new String[]{"时间", "日总业绩","日团队业绩","日个人业绩"});
		
		
		PageStructure data = PageUtil.createTablePageStructure("agent/yeji/listData", "id", tableHeader,null,null);
		
		Map<String,Object> map = Maps.newHashMap();
		map.put("formStruct", data);
		
		int week = DateUtil.dayOfWeek(new Date());
		Date dayw = DateUtil.addDay(new Date(), 7-week);
		String dayws = DateUtil.getDateStr(dayw);
		Map all = DaoUtils.getOneSql(Map.class,"select * from usr_day_rept where day=? and type=1 and uid =?",dayws, getUserId());
		if(all == null) {
			all = Maps.newHashMap();
		}
		all.put("xjjl", 0);
		map.put("formData", all);
		
		return createMV("agent/yeji","我的业绩", map);
	}
	
	/**
	 * 我的业绩：数据
	 * @param page
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="yeji/listData")
	public RespVO yejiData(Integer page,Integer limit) throws Exception {
		PageVO users = null;
		users = DaoUtils.getPageForMap(UsrDayRept.table(), null,page,limit,Where.eq("uid", getUserId()));
		
		return RespVO.createSuccessJsonResonse(users);
	}
	
	/**
	 * 我的奖励：结构
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="reward/list")
	public ModelAndView reward() throws Exception {
		TableHeader tableHeader = new TableHeader();
		tableHeader.setNames(new String[]{"update_time:date", "all_coin:yuan", "chou_coin:yuan","my_coin:yuan","reward:yuan","status"});
		tableHeader.setTexts(new String[]{"发送时间", "周总业绩","周团队业绩","周个人业绩","奖励","状态"});
		
		
		PageStructure data = PageUtil.createTablePageStructure("agent/reward/listData", "id", tableHeader,null,null);
		
		return createMV("tableList","我的奖励", Collections.singletonMap("formStruct", data));
	}
	
	/**
	 * 我的奖励：数据
	 * @param page
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="reward/listData")
	public RespVO rewardData(Integer page,Integer limit) throws Exception {
		PageVO users = null;
		users = DaoUtils.getPageForMap(UsrDayRept.table(), null,page,limit,Where.eq("uid", getUserId()),Where.eq("type", 1));
		users.getItems().forEach((it)->{
			it.put("status", MapUtils.getInteger(it, "status") == 0?"未发放":"已发放");
		});
		
		return RespVO.createSuccessJsonResonse(users);
	}
	
	/**
	 * 我的提现：结构
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="tixian/list")
	public ModelAndView tixian() throws Exception {
		TableHeader tableHeader = new TableHeader();
		tableHeader.setNames(new String[]{"id", "nick_name", "tdgx:yuan","wjgx:yuan","status"});
		tableHeader.setTexts(new String[]{"时间", "单号","金额","提现账号","状态"});
		
		
		PageStructure data = PageUtil.createTablePageStructure("agent/tixian/listData", "id", tableHeader,null,null);
		
		Map<String,Object> map = Maps.newHashMap();
		map.put("formStruct", data);
		
		int week = DateUtil.dayOfWeek(new Date());
		Date dayw = DateUtil.addDay(new Date(), 7-week);
		String dayws = DateUtil.getDateStr(dayw);
		Map all = DaoUtils.getOneSql(Map.class,"select * from usr_day_rept where day=? and type=1 and uid =?",dayws, getUserId());
		if(all == null) {
			all = Maps.newHashMap();
		}
		all.put("xjjl", 0);
		map.put("formData", all);
		
		return createMV("agent/tixian","我的提现", map);
	}
	
	/**
	 * 我的提现：数据
	 * @param page
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="tixian/listData")
	public RespVO tixianData(Integer page,Integer limit) throws Exception {
		PageVO users = null;
		users = DaoUtils.getPageForMap(UsrTixianLog.table(), null,page,limit,Where.eq("uid", getUserId()),Where.eq("method", 0));
		
		return RespVO.createSuccessJsonResonse(users);
	}
	
	/**
	 * 我的预提
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="yuti/list")
	public ModelAndView yuti() throws Exception {
		
		Map<String,Object> map = Maps.newHashMap();
		
		List<Map<String, Object>> users = DaoUtils.getListMap(UsrTixianLog.table(), null,100,Where.eq("uid", getUserId()),Where.eq("method", 1));
		
		map.put("formStruct", users );
		
		return createMV("agent/yuti","我的预提", map);
	}
	
	
	
	/**
	 * 设置为代理
	 * @param page
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="user/wbdfy")
	public RespVO wbdfy(Long id) throws Exception {
		if(id == null) return null;
		UsrUser my = (UsrUser) getUser();
		
		UsrUser user = DaoUtils.get(id, UsrUser.class);
		if(user == null) RespVO.createErrorJsonResonse("用户不存在");
		
		if(!user.getPuid().equals(my.getId()) || my.getAgent() <= 0) {
			return RespVO.createErrorJsonResonse("没有权限");
		}
		
		user.setAgent(my.getAgent()+1);
		DaoUtils.update(user);
		
		return RespVO.createSuccessJsonResonse("设置成功");
	}
	
	
	
}
